package net.hn.hnms.biz.intelligence.util;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author adam
 * @date 2020-09-08
 */
@Slf4j
public class DateRangeUtil {

    public static List<String> getDateRange(String startDate, String endDate, String pattern) {
        if (StringUtils.compare(startDate, endDate) >= 0) {
            return Collections.singletonList(startDate);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date start = null;
        try {
            start = simpleDateFormat.parse(startDate);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Collections.singletonList(startDate);
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);

        List<String> dateRange = Lists.newArrayList();
        String temp = startDate;
        while (StringUtils.compare(temp, endDate) <= 0) {
            try {
                dateRange.add(simpleDateFormat.format(simpleDateFormat.parse(temp)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            temp = simpleDateFormat.format(calendar.getTime());
        }

        return dateRange;
    }

}
